package eerbaar.v2.menu;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;

import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.IPosition;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.app.view.Standard;
import eerbaar.v2.graphics.Shadow;
import eerbaar.v2.graphics.Text;
import eerbaar.v2.graphics.rect.RectOp;
import eerbaar.v2.graphics.rect.RectRelation;
import eerbaar.v2.graphics.rect.Rectangle2;
import eerbaar.v2.menu.graphics.PositionFinder;
import eerbaar.v2.menu.graphics.ViewPort;

public class Menu {

    private static final Slide slide = new Slide();

    public Menu() {
        //
    }

    public void draw(Graphics2D graphics) {

        IGame game = GameHelper.getGame();
        if (game == null) {
            return;
        }

        Point topLeftPoint = getPetPoint(game);
        Rectangle2 petRect = new Rectangle2(topLeftPoint, new Dimension(50, 50));

        PetMenuProvider provider = PetMenuProvider.getInstance();

        Rectangle2 box = new Rectangle2(petRect.getCenter(),
                new Dimension(0, 0));
        MenuItem[] menus = provider.getMenuItems();
        for (MenuItem menu : menus) {

            Text text = new Text(menu.getText());
            Dimension textBox = text.getDimension(graphics);
            box = new Rectangle2(RectOp.join(box, textBox));
        }

        PositionFinder finder = new PositionFinder(ViewPort.DEFAULT, petRect);
        box = RectOp.expand(box, 7, 0);
        box = finder.position(box);
        Point topLeft = box.getTopLeft();

        for (MenuItem menu : menus) {

            Text text = new Text(menu.getText());
            Dimension textBox = text.getDimension(graphics);
            Rectangle2 cursor = new Rectangle2(topLeft, textBox);
            cursor = RectOp.expand(cursor, 7, 0);
            menu.setRectangle(cursor);

            Shadow shadow = new Shadow(cursor);
            shadow.drawShadow(graphics);

            topLeft = new Point((int) topLeft.getX(), (int) topLeft.getY()
                    + textBox.height);

            if (menu.isMouseOver()) {
                graphics.setColor(Standard.WHEAT);
            } else {
                graphics.setColor(Color.WHITE);
            }
            graphics.fillRoundRect(cursor.x, cursor.y, cursor.width,
                    cursor.height, 5, 5);

            graphics.setColor(Color.BLACK);
            graphics.drawString(menu.getText(), cursor.x + 3, cursor.y
                    + cursor.height - 4);
            if (menu.isMouseOver()) {
                graphics.setColor(Color.GRAY);
            } else {
                graphics.setColor(Color.BLACK);
            }
            graphics.drawRoundRect(cursor.x, cursor.y, cursor.width,
                    cursor.height, 5, 5);

            if (menu.isRunning()) {
                Menu.slide.draw(graphics, cursor);
            }
        }

    }

    public MenuItem findMenu(Point point) {

        MenuItem result = null;

        PetMenuProvider provider = PetMenuProvider.getInstance();
        MenuItem[] menus = provider.getMenuItems();
        for (MenuItem item : menus) {

            Rectangle2 rect = item.getRectangle();
            if (rect == null) {
                continue;
            }

            if (RectRelation.isInside(rect, point)) {
                result = item;
                break;
            }
        }

        return result;
    }

    private Point getPetPoint(IGame game) {

        IPersona pet = game.getPersona("pet");
        IPosition p = pet.getPosition();

        final int left = (p.getX() * 50);
        final int right = (p.getY() * 50);

        return new Point(left, right);
    }

    public boolean isInside(Point point) {

        return (findMenu(point) != null);
    }

    public void onClick(Point point) {

        MenuItem menu = findMenu(point);
        if (menu == null) {
            return;
        }

        menu.execute();
    }
}
